﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using GH.BLL;

/// <summary>
/// 员工管理画面
/// </summary>
public partial class VipMemberManage : GH.Web.BasePage.PageBase
{
   
    static string UnitRoot = System.Configuration.ConfigurationSettings.AppSettings["UnitRoot"];
    public string displayTitle = UnitRoot;

    #region 画面初始化

    static string strWhere = " 1=1";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            strWhere = " 1=1";
            //记载左边树图
            TreeViewInit();
            //加载员工信息
            DataShow();
        }
        else
        {
            DataShow();
        }
    }


    #endregion

    #region 画面方法

    /// <summary>
    /// 单位加载
    /// </summary>
    private void TreeViewInit()
    {
        trDepartMent.Nodes.Clear();

        //添加根节点
        TreeNode tnRoot = new TreeNode(UnitRoot, "0");
        trDepartMent.Nodes.Add(tnRoot);

        //添加单位信息
        Union bllUnion = new Union();
        TreeNode tnUnion;
        System.Text.StringBuilder strWhere = new System.Text.StringBuilder();
        //操作其他单位会员权限
        if (!this.GetRight(201))
        {
            strWhere.AppendFormat(" 工会单位信息.ID={0}", int.Parse(Session["Union"].ToString()));
        }
        else
        {
            strWhere.AppendFormat("");
        }
        DataTable dtQuery = bllUnion.GetList(strWhere.ToString()).Tables[0];
        if (dtQuery.Rows.Count != 0)
        {
            for (int i = 0; i < dtQuery.Rows.Count; i++)
            {
                tnUnion = new TreeNode();
                tnUnion.Text = dtQuery.Rows[i]["单位名称"].ToString();
                tnUnion.Value = dtQuery.Rows[i]["ID"].ToString();
                trDepartMent.Nodes[0].ChildNodes.Add(tnUnion);
            }
            trDepartMent.Nodes[0].Expand();
        }
        //添加部门信息
        DepartMent bllDepartMent = new DepartMent();
        //班组信息
        Group bllGroup = new Group();
        TreeNode tnDepartMent;
        for (int intUion = 0; intUion < trDepartMent.Nodes[0].ChildNodes.Count; intUion++)
        {
            //部门
            dtQuery = bllDepartMent.GetList("单位ID=" + trDepartMent.Nodes[0].ChildNodes[intUion].Value).Tables[0];
            for (int i = 0; i < dtQuery.Rows.Count; i++)
            {
                tnDepartMent = new TreeNode();
                tnDepartMent.Text = dtQuery.Rows[i]["部门名称"].ToString();
                tnDepartMent.Value = "M" + dtQuery.Rows[i]["ID"].ToString();
                trDepartMent.Nodes[0].ChildNodes[intUion].ChildNodes.Add(tnDepartMent);
            }
            //班组
            dtQuery = bllGroup.GetList("单位ID=" + trDepartMent.Nodes[0].ChildNodes[intUion].Value).Tables[0];
            for (int i = 0; i < dtQuery.Rows.Count; i++)
            {
                tnDepartMent = new TreeNode();
                tnDepartMent.Text = dtQuery.Rows[i]["班组名称"].ToString();
                tnDepartMent.Value = "Z" + dtQuery.Rows[i]["ID"].ToString();
                trDepartMent.Nodes[0].ChildNodes[intUion].ChildNodes.Add(tnDepartMent);
            }
        }
        //默认合并
        trDepartMent.ExpandDepth = 0;
        //权限控制
        if (trDepartMent.Nodes[0].ChildNodes.Count == 1)
        {
            trDepartMent.Nodes[0].ChildNodes[0].Select();
        }
     }

    /// <summary>
    /// 画面数据显示
    /// </summary>
    private void DataShow()
    {
        DataSet dsResult = new DataSet();
        VipMember bllVip = new VipMember();
        //操作其他单位会员权限
        if (!this.GetRight(201))
        {
            strWhere=strWhere+" AND 单位="+Session["Union"].ToString();
        }
        dsResult = bllVip.GetListWhere(strWhere);
        this.dlVipMember.DataSource = dsResult;
        lblDataCount.Text = dsResult.Tables[0].Rows.Count.ToString();
        this.dlVipMember.DataBind();
    }

    #endregion

    #region  画面事件

    protected void btnInsert_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("VipMemberAdd.aspx");
    }

    protected void trDepartMent_SelectedNodeChanged(object sender, EventArgs e)
    {
        
        int depth = trDepartMent.SelectedNode.Depth;
        string value = this.trDepartMent.SelectedNode.Value;
        if(depth ==0)
        {
            displayTitle = UnitRoot;
            //权限控制
            if (trDepartMent.Nodes[0].ChildNodes.Count == 1)
            {
                trDepartMent.Nodes[0].ChildNodes[0].Select();
                this.trDepartMent.SelectedNode.Text = "<b>" + this.trDepartMent.SelectedNode.Text + "</b>";
                return;
            }
        }
        if (depth == 1)
        {
            displayTitle = UnitRoot+">>"+this.trDepartMent.SelectedNode.Text;
            strWhere = "[单位]=" + value;
        }
        else if (depth == 2)
        {
            displayTitle = UnitRoot+">>" +this.trDepartMent.SelectedNode.Parent.Text+">>"+ this.trDepartMent.SelectedNode.Text;
            System.Text.StringBuilder strSql = new System.Text.StringBuilder();
            strSql.AppendFormat("单位={0}", trDepartMent.SelectedNode.Parent.Value);
            if (trDepartMent.SelectedNode.Value.Substring(0, 1) == "M")
            {
                strSql.AppendFormat(" AND 部门={0}", trDepartMent.SelectedNode.Value.Substring(1, trDepartMent.SelectedNode.Value.Length - 1));
            }
            else
            {
                strSql.AppendFormat(" AND 班组={0}", trDepartMent.SelectedNode.Value.Substring(1, trDepartMent.SelectedNode.Value.Length - 1));
            }

            strWhere = strSql.ToString();
        }
        else if (depth == 0)
        {
            strWhere = "1=1";
        }
        DataShow();

        string path = this.didDep.Value;
        string theValue = this.DidValue.Value;

        if (path != "")
        {
            TreeNode node = trDepartMent.FindNode(path);
            node.Text = theValue;
        }
        path = this.trDepartMent.SelectedNode.ValuePath;

        theValue = this.trDepartMent.SelectedNode.Text;

        this.trDepartMent.SelectedNode.Text="<b>"+this.trDepartMent.SelectedNode.Text+"</b>";

        this.didDep.Value = path;
        this.DidValue.Value = theValue;

        //DataSet dsResult = new DataSet();
        //VipMember bllVip = new VipMember();
        //if (trDepartMent.SelectedNode.Depth == 0)
        //{
        //    dsResult = bllVip.GetAllList();
        //}
        //else if (trDepartMent.SelectedNode.Depth == 1)
        //{
        //    dsResult = bllVip.GetListWhere("单位=" + trDepartMent.SelectedNode.Value);
        //}
        //else
        //{
        //    System.Text.StringBuilder strWhere = new System.Text.StringBuilder();
        //    strWhere.AppendFormat("单位={0}", trDepartMent.SelectedNode.Parent.Value);
        //    if (trDepartMent.SelectedNode.Value.Substring(0, 1) == "0")
        //    {
        //        strWhere.AppendFormat(" AND 部门={0}", trDepartMent.SelectedNode.Value.Substring(1, trDepartMent.SelectedNode.Value.Length - 1));
        //    }
        //    else
        //    {
        //        strWhere.AppendFormat(" AND 班组={0}", trDepartMent.SelectedNode.Value.Substring(1, trDepartMent.SelectedNode.Value.Length - 1));
        //    }

        //    dsResult = bllVip.GetListWhere(strWhere.ToString());
        //}
        //this.dlVipMember.DataSource = dsResult;
        //this.dlVipMember.DataBind();

   }

    #endregion


}

